<template>
  <div
    class="category-cell van-hairline--surround"
    :class="customClass"
  >
    <div class="category-cell-title">
      <image
        :src="icon"
        mode="aspectFill"
      />
      <div class="category-cell-title__label">{{title}}</div>
    </div>
    <div class="category-cell-content">
      <navigator
        class="category-cell-content__item"
        v-for="(item,index) in items"
        :key="index"
        :url="item.url"
      >
        {{item.title}}
      </navigator>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CateCell',
  props: {
    items: {
      type: Array,
      default: []
    },
    customClass: String,
    title: String,
    icon: String,
    columnNum: {
      type: Number,
      default: 2
    },
    border: {
      type: Boolean,
      default: true
    },
    gutter: [Number, String],
    background: String,
    iconWidth: [Number, String],
    iconHeight: [Number, String]
  },
  computed: {
    gridStyle () {
      if (this.gutter) {
        let padding = this.addUnit(this.gutter)
        return `padding: ${padding};`
      }
      return false
    },
    columStyle () {
      let style = []
      if (this.columnNum) {
        let width = 100 / this.columnNum
        style.push(`width:${width}%`)
      }
      if (this.gutter) {
        let padding = this.addUnit(this.gutter)
        style.push(`padding:${padding}`)
      }
      return style.join(';')
    },
    backgroundStyle () {
      return this.background ? `background: ${this.background};` : false
    },
    iconStyle () {
      let style = []
      if (this.iconWidth) {
        let width = this.addUnit(this.iconWidth)
        style.push(`width:${width}`)
      }
      if (this.iconHeight) {
        let height = this.addUnit(this.iconHeight)
        style.push(`height:${height}`)
      }
      return style.join(';')
    }
  },
  methods: {
    addUnit (value) {
      if (value) {
        return /^\d+(\.\d+)?$/.test(value) ? value + 'px' : value
      }
      return undefined
    }
  }
}
</script>

<style lang="less" scoped>
.category-cell {
  padding: 10px 0;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 15px;
  &::after {
    border-radius: 5px;
  }
  &:last-child {
    margin: 0;
  }
  &-title {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    image {
      width: 32px;
      height: 32px;
    }
    &__label {
      font-size: 12px;
      font-weight: bold;
      padding: 5px 0;
    }
  }
  &-content {
    align-self: flex-start;
    flex: 3;
    display: flex;
    flex-wrap: wrap;
    &__item {
      flex-basis: 33%;
      font-size: 12px;
      text-align: center;
      padding: 5px 0;
      position: relative;
      &:nth-of-type(n + 4) {
        margin-top: 5px;
      }
      &:not(:last-child):not(:nth-child(3n + 0))::after {
        position: absolute;
        right: 0;
        top: 20%;
        width: 1px;
        height: 60%;
        transform: scale(0.5);
        background: #f1f2f3;
        content: "";
      }
    }
  }
}
</style>
